from nsga2.cnsga2 import CNSGA2
from problems.rcm21 import RCM21
import numpy as np
import matplotlib.pyplot as plt

def run_rcm21():
    rcm21 = RCM21()

    # random sampling for feasible area
    n_samples = 100000
    X = np.random.uniform(low=rcm21.lower, high=rcm21.upper, size=(n_samples, rcm21.n_var))
    F, C = rcm21.evaluate(X)
    feasible_mask = np.all(C <= 1e-6, axis=1)
    F_feasible = F[feasible_mask]

    # run CNSGA2
    cnsga2 = CNSGA2(
        pop_size=200,
        n_var=rcm21.n_var,
        pc=0.9,
        pm=0.1
    )
    population_x, optimum_fx = cnsga2.run(rcm21, max_gen=100)

    # plot once
    plt.figure()
    plt.scatter(F_feasible[:, 0], F_feasible[:, 1], s=10, label='Feasible Area', alpha=0.5)
    plt.scatter(optimum_fx[:, 0], optimum_fx[:, 1], color='red', s=30, label='Optimal Solutions')
    plt.xlabel('f1')
    plt.ylabel('f2')
    plt.title('RCM21: Feasible Area & Optimal Solutions')
    plt.legend()
    plt.grid(True)
    plt.tight_layout()
    plt.show()

if __name__ == "__main__":
    run_rcm21()
